package com.elitel.frame.base.service.mongodb;

import java.util.ArrayList;
import java.util.List;

import com.elitel.common.mongodb.GeneralCallInterfaceRecord;
//import com.elitel.common.mongodb.GeneralCallInterfaceRecordRepository;
import com.elitel.common.utils.XCommonUtils;
import com.elitel.frame.main.entity.vo.JsonDataResult;
import com.elitel.frame.main.entity.vo.JsonResult;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
//import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Projections.*;
import static com.mongodb.client.model.Sorts.descending;


import lombok.extern.slf4j.Slf4j;

@Service
public class GeneralMongoService{

    // @Autowired
    // private MongoTemplate mongoTemplate;

//    @Autowired
    private MongoClient mongoClient;

    @Value("${mongodb.config.default.databaseName}")
    private String defaultMongoDatabaseName;

    public Object findAllByPagination(Integer pageNo, Integer pageSize, Long executeTimeCondition, String serviceId){

        if(mongoClient == null)
            return null;
        pageNo = pageNo == null ? 0 : pageNo;
        pageSize = pageSize == null? 0 : 10;
        executeTimeCondition = executeTimeCondition==null?0:executeTimeCondition;

        MongoCollection<Document> recordCollection =  getCallInterfaceRecordCollection();
        Bson executeTimeBson = gte("executeTime", executeTimeCondition);
        Bson totalCondition = null;

        if(XCommonUtils.isNotEmpty(serviceId)){
            totalCondition = and(eq("serviceID", serviceId), executeTimeBson);
        }else{
            totalCondition = executeTimeBson;
        }

        List<Document> docs = recordCollection.find(totalCondition)
                                                .sort(descending("timestamp"))
                                                .skip(pageNo-1)
                                                .limit(pageSize)
                                                .into(new ArrayList<>());

        long size = recordCollection.countDocuments(totalCondition);

        PageResponseVo<Document> pageResponseVo = new PageResponseVo();
        pageResponseVo.setRows(docs);
        pageResponseVo.setTotal((int)size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(pageResponseVo);
        jsonResult.setSuccess(true);
        return jsonResult;

    }

    private MongoCollection<Document> getCallInterfaceRecordCollection(){
        return getDefaultMongoDB().getCollection("callInterfaceRecord");
    }

    private MongoDatabase getDefaultMongoDB(){
       return mongoClient.getDatabase(defaultMongoDatabaseName);
    }
}
